﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lyh.Train.Common.Message;
using Lyh.Train.Controls.Editors;

namespace Lyh.Train.UI
{
    public partial class BaseForm : Form
    {
        public delegate void MessegeDisplayHandler(ColorTextBox textBox, string text, Color color);

        public delegate void MessegeBoxHandler(string text);

        public delegate void ImageDisplayHandler(PictureBox pb, Image img);

        public delegate void ControlShowHandler(Control ctr, bool visible);

        public delegate void CommonHandler(object obj);

        public bool EnableNotifyIcon { get; set; }

        public BaseForm()
        {
            EnableNotifyIcon = false;
            InitializeComponent();
        }

        #region --公用方法--

        private void SetText(ColorTextBox textBox, string text, Color color)
        {
            textBox.AppendText(Environment.NewLine);
            if (textBox.Lines.Count() > 500) textBox.Clear();
            text = string.Format("[{0}]{1}", DateTime.Now.ToString("HH:mm:ss.fff"), text);
            textBox.AppendText(text, color);
        }

        private void DisplayText(object obj)
        {
            var item = obj as TextItem;
            if (item == null) return;
            if (item.Location == NewLineLocation.Before) item.TextBox.AppendText(item.NewLine);
            if (item.TextBox.Lines.Count() > 500) item.TextBox.Clear();
            item.TextBox.AppendText(item.Text, item.ForeColor);
            if (item.Location == NewLineLocation.After) item.TextBox.AppendText(item.NewLine);
        }

        protected void Output(ColorTextBox textBox, string text, Color color)
        {
            Invoke(new MessegeDisplayHandler(SetText), textBox, text, color);
        }

        private void SetBox(string text)
        {
            MessageBox.Show(text, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        protected void Alert(string text)
        {
            if (InvokeRequired) Invoke(new MessegeBoxHandler(SetBox), text);
            else SetBox(text);
        }

        private void SetImage(PictureBox pb, Image img)
        {
            pb.Image = img;
        }

        public void InvokeSetImage(PictureBox pb, Image img)
        {
            Invoke(new ImageDisplayHandler(SetImage), pb, img);
        }

        public void ShowControl(Control ctr, bool visible)
        {
            ctr.Visible = visible;
        }

        public void InvokeShowControl(Control ctr, bool visible)
        {
            Invoke(new ControlShowHandler(ShowControl), ctr, visible);
        }

        #endregion

        private void BaseForm_Load(object sender, EventArgs e)
        {

        }

        private void ShowNotifyIcon(bool show)
        {
            Visible = !show;
            ShowInTaskbar = !show;
            notifyIcon.Visible = show;
            notifyIcon.ShowBalloonTip(3000);
            Visible = !show;
            if (Visible) Activate();
        }

        private void tsmiExit_Click(object sender, EventArgs e)
        {
            ConfirmExit();
        }

        public void ConfirmExit()
        {
            if (DialogResult.OK == MessageBoxHelper.Confirm("确定退出？"))
            {
                Application.Exit();
            }
        }

        private void tsmiAbout_Click(object sender, EventArgs e)
        {
            About();
        }

        public void About()
        {
            using (var frm = new AboutForm())
            {
                frm.ShowDialog();
            }
        }

        private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ShowNotifyIcon(false);
            }
        }

        private void BaseForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (EnableNotifyIcon)
            {
                if (e.CloseReason == CloseReason.UserClosing)
                {
                    e.Cancel = true;
                    ShowNotifyIcon(true);
                }
            }
        }
    }
}
